

clear
set more off


use "$data\MS_final_dataset.dta" , clear

destring ym, replace
gen year= ym 

* Treatment/Origin
 
gen nat= wbc
drop _merge
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
replace EUII=4 if nat==224 
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
recode EUII (.=0)
lab define eui3w 0 "Italian" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3w
rename EUII EUIIW  
drop nat

gen nat=hbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
replace EUII=4 if nat==224 
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
recode EUII (.=0)
lab define eui3h 0 "Italian" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3h
rename EUII EUIIH  
drop nat

drop if msdate==.|delta_age==.|EUIIW==.|EUIIH==.
gen sepyear = yofd(date(sepdate, "DMY"))


* Generate dummies for sorting on attributes 

gen woman10younger=delta_age>=10
gen woman10older=delta_age<=-10
gen sameduc=delta_edu==0
gen dnat_ita_ita=(EUIIH==0&EUIIW==0)
gen dnat_othersnew_othersnew=(EUIIH!=0&EUIIW!=0)
gen dnat_ita_others=((EUIIH==0&EUIIW!=0)|(EUIIW==0&EUIIH!=0)) 


*  Consider 3 cohorts:

*  1. marriages formed 1998-1999 and separations through 2002 --> pre-enlargement 

preserve
keep if ym>=1998&ym<=1999
gen dnat_ita_neweu= ((EUIIH==0&(EUIIW==2|EUIIW==3))|(EUIIW==0&(EUIIW==2|EUIIW==3)))
replace dnat_ita_others=0 if ((EUIIH==0&(EUIIW==2|EUIIW==3))|(EUIIW==0&(EUIIW==2|EUIIW==3)))
gen sepdummy=(separated==1&(sepyear>=2000&sepyear<=2001))
keep dnat_* sepdummy ym sameduc provres woman10younger woman10older
gen post=0
save "$data\moments_separations_for_structural_model.dta", replace
restore

*  2. marriages formed 2002-2003 and separations through 2005 --> post-EU2004 

preserve
keep if ym>=2002&ym<=2003
gen dnat_ita_neweu=((EUIIH==0&EUIIW==2)|(EUIIW==0&EUIIH==2))
replace dnat_ita_others=0 if ((EUIIH==0&EUIIW==2)|(EUIIW==0&EUIIH==2))
gen sepdummy=(separated==1&(sepyear>=2004&sepyear<=2005))
keep dnat_* sepdummy ym sameduc provres woman10younger woman10older
append using "$data\moments_separations_for_structural_model.dta"
save "$data\moments_separations_for_structural_model.dta", replace
restore

*  3. marriages formed 2005-2006 and separations through 2008 --> post-EU2007 

preserve
keep if ym>=2005&ym<=2006
gen dnat_ita_neweu=((EUIIH==0&EUIIW==3)|(EUIIW==0&EUIIH==3))
replace dnat_ita_others=0 if ((EUIIH==0&EUIIW==3)|(EUIIW==0&EUIIH==3))
gen sepdummy=(separated==1&(sepyear>=2007&sepyear<=2008))
keep dnat_* sepdummy ym sameduc provres woman10younger woman10older
append using "$data\moments_separations_for_structural_model.dta"
gen enlargement_round=1 if ym>=2002&ym<=2003
replace enlargement_round=2 if ym>=2005&ym<=2006
mvencode post, mv(1)
save "$data\moments_separations_for_structural_model.dta", replace
restore


* Generate estimates for structural model:  moments_separations_3cohorts.csv
use "$data\moments_separations_for_structural_model.dta", clear

* marriages of EU2004-2007 & Italians formed in 1998-1999 and separating in 2000-2001 
sum sepdummy if dnat_ita_ita & post==0
areg sepdummy dnat_ita_neweu dnat_ita_others dnat_othersnew_othersnew woman* sameduc if post==0, abs(provres) robust cluster(provres)

* marriages of EU2004 & Italians in 2002-2003 and separating in 2004-2005 + marriages of EU2007 & Italians in 2005-2006 and separating in 2007-2008 
sum sepdummy if dnat_ita_ita & post==1
areg sepdummy dnat_ita_neweu dnat_ita_others dnat_othersnew_othersnew woman* sameduc if post==1, abs(provres) robust cluster(provres)

sum sepdummy if dnat_ita_ita&enlarg==1&post==1
areg sepdummy dnat_ita_neweu dnat_ita_others dnat_othersnew_othersnew woman* sameduc if enlarg==1&post==1, abs(provres) robust cluster(provres)

sum sepdummy if dnat_ita_ita&enlarg==2&post==1
areg sepdummy dnat_ita_neweu dnat_ita_others dnat_othersnew_othersnew woman* sameduc if enlarg==2&post==1, abs(provres) robust cluster(provres)

